package com.android.cherryboard;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.ashu.ProfileActivity;
import com.android.cherryboard.Deshboard.CherryfullDashboardActivity;
import com.android.cherryboard.UI.LoginButton;
import com.android.cherryboard.Utility.Utility;
import com.android.cherryboard.fbclass.BaseRequestListener;
import com.android.cherryboard.fbclass.SessionEvents;
import com.android.cherryboard.fbclass.SessionStore;
import com.android.cherryboard.fbclass.SessionEvents.AuthListener;
import com.android.cherryboard.fbclass.SessionEvents.LogoutListener;
import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;
import com.facebook.android.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.DashPathEffect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
import android.widget.Toast;

public class SplashActivity extends Activity {
	/** Called when the activity is first created. */

	public static final String APP_ID = "237306236398903";
	private Handler mHandler;
	final static int AUTHORIZE_ACTIVITY_RESULT_CODE = 0;
	String[] permissions = { "offline_access", "publish_stream", "user_photos",
			"publish_checkins", "photo_upload" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);

		setUpView();

	}

	private void setUpView() {
		// TODO Auto-generated method stub

		mHandler = new Handler();

		Utility.mFacebook = new Facebook(APP_ID);
		Utility.mAsyncRunner = new AsyncFacebookRunner(Utility.mFacebook);
		LoginButton mLoginButton = (LoginButton) findViewById(R.id.loginButton_splash_screen);

		// restore session if one exists
		SessionStore.restore(Utility.mFacebook, this);
		SessionEvents.addAuthListener(new FbAPIsAuthListener());
		SessionEvents.addLogoutListener(new FbAPIsLogoutListener());
		/*
		 * Source Tag: login_tag
		 */
		mLoginButton.init(this, AUTHORIZE_ACTIVITY_RESULT_CODE,
				Utility.mFacebook, permissions);

		if (Utility.mFacebook.isSessionValid()) {
			requestUserData();
		}

	}

	private void requestUserData() {
		// TODO Auto-generated method stub
		/* mText.setText("Fetching user name, profile pic..."); */
		Bundle params = new Bundle();
		params.putString("fields", "name, picture");
		Utility.mAsyncRunner.request("me", params, new UserRequestListener());

	}

	@Override
	public void onResume() {
		super.onResume();
		if (Utility.mFacebook != null) {
			if (!Utility.mFacebook.isSessionValid()) {
				/*
				 * mText.setText("You are logged out! ");
				 * mUserPic.setImageBitmap(null);
				 * 
				 * btn_getFriends.setOnClickListener(null);
				 * 
				 * 
				 * btnPostStatus.setClickable(false);
				 * btnPostStatus.setOnClickListener(null);
				 */
				Toast.makeText(SplashActivity.this, "You are logged out",
						Toast.LENGTH_SHORT).show();

			} else {
				Utility.mFacebook.extendAccessTokenIfNeeded(this, null);
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		/*
		 * if this is the activity result from authorization flow, do a call
		 * back to authorizeCallback Source Tag: login_tag
		 */

		case AUTHORIZE_ACTIVITY_RESULT_CODE: {
			Utility.mFacebook.authorizeCallback(requestCode, resultCode, data);
			break;
		}
		}
	}

	/*
	 * Callback for fetching current user's name, picture, uid.
	 */
	public class UserRequestListener extends BaseRequestListener {

		@Override
		public void onComplete(final String response, final Object state) {
			JSONObject jsonObject;
			try {
				jsonObject = new JSONObject(response);

				final String picURL = jsonObject.getJSONObject("picture")
						.getJSONObject("data").getString("url");
				final String name = jsonObject.getString("name");
				Utility.userUID = jsonObject.getString("id");
				Utility.userName = name;
				Utility.userPicURL = picURL;
				System.out.println(" Utility.userUID   " + Utility.userUID);
			//	Intent dashboardIntent = new Intent(SplashActivity.this, CherryfullDashboardActivity.class);
				Intent dashboardIntent = new Intent(SplashActivity.this, ProfileActivity.class);
				startActivity(dashboardIntent );

				/*
				 * mHandler.post(new Runnable() {
				 * 
				 * @Override public void run() {
				 * 
				 * mText.setText("Welcome " + name + "!");
				 * mUserPic.setImageBitmap(Utility.getBitmap(picURL));
				 * 
				 * btnPostStatus.setClickable(true);
				 * btnPostStatus.setOnClickListener(MainActivity.this);
				 * btn_getFriends.setOnClickListener(MainActivity.this);
				 * 
				 * Toast.makeText(SplashActivity.this, name ,
				 * Toast.LENGTH_SHORT).show();
				 * 
				 * } });
				 */

			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}

	/*
	 * The Callback for notifying the application when authorization succeeds or
	 * fails.
	 */

	public class FbAPIsAuthListener implements AuthListener {

		@Override
		public void onAuthSucceed() {
			requestUserData();
		}

		@Override
		public void onAuthFail(String error) {
			/*
			 * mText.setText("Login Failed: " + error);
			 * btn_getFriends.setOnClickListener(null);
			 * 
			 * 
			 * btnPostStatus.setClickable(false);
			 * btnPostStatus.setOnClickListener(null);
			 */
			Toast.makeText(SplashActivity.this, "onAuthFail",
					Toast.LENGTH_SHORT).show();
		}
	}

	/*
	 * The Callback for notifying the application when log out starts and
	 * finishes.
	 */

	public class FbAPIsLogoutListener implements LogoutListener {
		@Override
		public void onLogoutBegin() {
			/* mText.setText("Logging out..."); */
		}

		@Override
		public void onLogoutFinish() {
			/*
			 * mText.setText("You have logged out! ");
			 * mUserPic.setImageBitmap(null);
			 * btn_getFriends.setOnClickListener(null);
			 * 
			 * 
			 * btnPostStatus.setClickable(false);
			 * btnPostStatus.setOnClickListener(null);
			 */
			Toast.makeText(SplashActivity.this, "onLogoutFinish",
					Toast.LENGTH_SHORT).show();

		}
	}

}